<?php
class monitor_model extends CI_Model {

	public function __construct(){
		parent::__construct();
		$this->db = $this->load->database('default',true);
	}

	public function save_monitor($monitor) {
		$this->db->insert("monitor",$monitor);
		return $this->db->insert_id();
	}
	
	public function query_monitor_list($user_id,$start,$limit,$search){
		if ($user_id==1) {
			$sql ="select
						monitor_id,
						monitor.media_id,
						monitor.industry,
						monitor.industry_category,
						industry.industry_name as industry_name,
						industry_category.industry_name as industry_category_name,
						product_name,
						monitor_time
					from 	monitor 
						inner join sys_industry as industry  on monitor.industry =industry.industry_id
						inner join sys_industry as industry_category  on monitor.industry_category =industry_category.industry_id
					";
		}else{
			$sql ="select
						monitor_id,
						monitor.media_id,
						monitor.industry,
						monitor.industry_category,
						industry.industry_name as industry_name,
						industry_category.industry_name as industry_category_name,
						product_name,
						monitor_time
					from 	monitor
						inner join media on media.media_id =monitor.media_id
						inner join adm_user_region as aur on media.province=aur.region_id
						inner join sys_industry as industry  on monitor.industry =industry.industry_id
						inner join sys_industry as industry_category  on monitor.industry_category =industry_category.industry_id
					";
		}
		
		$join_media =false;//连接media表
		$join_brand =false;//连接品牌表
		$join_media_env =false;//连接媒体点位
		
		$search_path =" ";
		
		if ($search['industry_category']) {
			$industry_category =$this->db->escape($search['industry_category']);
			$search_path .=" and  industry_category =$industry_category";
		}elseif ($search['industry']) {
			$industry =$this->db->escape($search['industry']);
			$search_path .=" and  industry =$industry ";
		}
		
		if ($search['region']) {
			$join_media=true;
			$region =$this->db->escape($search['region']);
			$search_path .=" and  media.region =$region";
		}elseif ($search['city']){
			$join_media=true;
			$city =$this->db->escape($search['city']);
			$search_path .=" and  media.city =$city";
		}elseif ($search['province']){
			$join_media=true;
			$province =$this->db->escape($search['province']);
			$search_path .=" and  media.province =$province";
		}
		
		if ($search['media_type_id']) {
			$join_media=true;
			$media_type_id =$this->db->escape($search['media_type_id']);
			$search_path .=" and  media.media_type_id =$media_type_id";
		}
		
		if ($search['monitor_time']) {
			$monitor_time =$this->db->escape($search['monitor_time']);
			$search_path .=" and date(monitor_time) =$monitor_time";
		}
		
		if ($search['product_name']) {
			$product_name =$this->db->escape_like_str($search['product_name']);
			$search_path .=" and product_name like '%$product_name%'";
		}
		
		
		if ($search['brand']) {
			$join_brand=true;
			$brand =$this->db->escape($search['brand']);
			$search_path .=" and brand =$brand";
		}
		
		
		if ($search['media_type']) {
			$join_media=true;
			$media_type =$this->db->escape($search['media_type']);
			$search_path .=" and media_type =$media_type";
		}
		
		if ($search['dianwei']) {
			$join_media=true;
			$join_media_env=true;
			$dianwei_sql =" and (1=0 ";
			foreach ($search['dianwei'] as $dianwei){
					$dianwei_sql .=" or env_type ='$dianwei'";
			}
			$search_path .=$dianwei_sql ." )";
		}
		if ($join_media) {
			$sql =$sql." inner join media on monitor.media_id =media.media_id ";
		}
		if ($join_brand) {
			$sql =$sql." inner join sys_brand on sys_brand.brand_id =monitor.brand ";
		}
		if ($join_media_env) {
			$sql =$sql." inner join media_env on  media.media_id =media_env.media_id ";
		}
		$sql =$sql."where  1=1 ";
		if ($user_id!=1) {
			$sql  =$sql." and  aur.user_id ='$user_id'";
		}
		$sql =$sql.$search_path;
		$sql =$sql."  order by monitor_time desc limit $start,$limit";
		$query =$this->db->query($sql);
		if ($query){
			return $query->result_array();
		}
	}
	
	public function query_monitor_num($user_id,$search) {
		if ($user_id==1) {
			$sql ="select
						count(monitor_id) as num
					from 	monitor 
						inner join sys_industry as industry  on monitor.industry =industry.industry_id
						inner join sys_industry as industry_category  on monitor.industry_category =industry_category.industry_id
					";
		}else{
			$sql ="select
						count(monitor_id) as num
					from 	monitor
						inner join media on media.media_id =monitor.media_id
						inner join adm_user_region as aur on media.province=aur.region_id
						inner join sys_industry as industry  on monitor.industry =industry.industry_id
						inner join sys_industry as industry_category  on monitor.industry_category =industry_category.industry_id
					";
		}
		
		$join_media =false;//连接media表
		$join_brand =false;//连接品牌表
		$join_media_env =false;//连接媒体点位
		
		$search_path =" ";
		
		if ($search['industry_category']) {
			$industry_category =$this->db->escape($search['industry_category']);
			$search_path .=" and  industry_category =$industry_category";
		}elseif ($search['industry']) {
			$industry =$this->db->escape($search['industry']);
			$search_path .=" and  industry =$industry ";
		}
		
		if ($search['region']) {
			$join_media=true;
			$region =$this->db->escape($search['region']);
			$search_path .=" and  media.region =$region";
		}elseif ($search['city']){
			$join_media=true;
			$city =$this->db->escape($search['city']);
			$search_path .=" and  media.city =$city";
		}elseif ($search['province']){
			$join_media=true;
			$province =$this->db->escape($search['province']);
			$search_path .=" and  media.province =$province";
		}
		
		if ($search['media_type_id']) {
			$join_media=true;
			$media_type_id =$this->db->escape($search['media_type_id']);
			$search_path .=" and  media.media_type_id =$media_type_id";
		}
		
		if ($search['monitor_time']) {
			$monitor_time =$this->db->escape($search['monitor_time']);
			$search_path .=" and date(monitor_time) =$monitor_time";
		}
		
		if ($search['product_name']) {
			$product_name =$this->db->escape_like_str($search['product_name']);
			$search_path .=" and product_name like '%$product_name%'";
		}
		
		
		if ($search['brand']) {
			$join_brand=true;
			$brand =$this->db->escape($search['brand']);
			$search_path .=" and brand =$brand";
		}
		
		
		if ($search['media_type']) {
			$join_media=true;
			$media_type =$this->db->escape($search['media_type']);
			$search_path .=" and media_type =$media_type";
		}
		
		if ($search['dianwei']) {
			$join_media=true;
			$join_media_env=true;
			$dianwei_sql =" and (1=0 ";
			foreach ($search['dianwei'] as $dianwei){
					$dianwei_sql .=" or env_type ='$dianwei'";
			}
			$search_path .=$dianwei_sql ." )";
		}
		if ($join_media) {
			$sql =$sql." inner join media on monitor.media_id =media.media_id ";
		}
		if ($join_brand) {
			$sql =$sql." inner join sys_brand on sys_brand.brand_id =monitor.brand ";
		}
		if ($join_media_env) {
			$sql =$sql." inner join media_env on  media.media_id =media_env.media_id ";
		}
		$sql =$sql."where  1=1 ";
		if ($user_id!=1) {
			$sql  =$sql."and aur.user_id ='$user_id'";
		}
		$sql =$sql.$search_path;
		$query =$this->db->query($sql);
		if ($query){
			$row =$query->row_array();
			return $row['num'];
		}
	}
	
	public function query_monitor($monitor_id) {
		$monitor_id =$this->db->escape_str($monitor_id);
		$sql ="select 	monitor.*,brand_name  from monitor left  join sys_brand  on monitor.brand =sys_brand.brand_id  where monitor_id ='$monitor_id' limit 1";
		$query =$this->db->query($sql);
		if ($query){
			return $query->row_array();
		}
	}
	
	public function query_monitor_detail($monitor_id){
		$monitor_id =$this->db->escape_str($monitor_id);
		$sql ="select 	monitor.*,
					 	si1.industry_name as industry_name,	 
					 	si2.industry_name as industry_category_name	,
					 	brand_name 
				from monitor 
					inner join sys_industry as si1 on monitor.industry =si1.industry_id
					inner join sys_industry as si2 on monitor.industry_category =si2.industry_id
					left  join sys_brand  on monitor.brand =sys_brand.brand_id
				where monitor_id ='$monitor_id' limit 1";
		$query =$this->db->query($sql);
		if ($query){
			return $query->row_array();
		}
	}
	
	public function update_monitor($monitor,$monitor_id) {
		$this->db->where("monitor_id",$monitor_id);
		$this->db->update("monitor",$monitor);
	}
	
	public function delete_monitor($monitor_id) {
		$monitor_id =$this->db->escape_str($monitor_id);
		$sql ="delete from monitor where monitor_id ='$monitor_id'";
		$this->db->query($sql);
	}
}